
//var isiOs = false;

var backMusic = createAudio("audio/back.mp3");
var buttonSound = createAudio("audio/button.mp3");
var end_tour = createAudio("audio/end_tour.mp3");
var notGuessingSound = createAudio("audio/not_guessing.wav");
var guessingSound = createAudio("audio/guessing.mp3");
var newWordSound = createAudio("audio/new_word.mp3");
var writingSound = createAudio("audio/writing.mp3");
var clockSound = createAudio("audio/clock.mp3");

var isSound = true;
var isMusic = true;

var isSendRequest = false;
var requests = [];

function sendRequest(act, p){
	requests.push({action : act, param : p});
	sendAllRequests();
}

function sendAllRequests(){
	if(!isSendRequest && requests.length > 0){
		isSendRequest = true;
		
		var link = "developer_call?"+JSON.stringify(requests);
		delete requests;
		requests = [];
		//console.log(link);
		location.replace(link);
		
		//setTimeout('receiveRequest()', 2000);
	}
}

function receiveRequest(){
	isSendRequest = false;
	sendAllRequests();
}

function createAudio(src){
	var sound;
	if(Device.isiOS()){
		sound = src;
	} else {
		sound = new Audio();
		sound.setAttribute("src", src);
		sound.load();	
	}
	return sound;
}

function setSoundVolume(volume){
	if(Device.isiOS()){
		sendRequest('soundVolume', volume);
	} else {
		buttonSound.volume = volume;
		end_tour.volume = volume;
		notGuessingSound.volume = volume;
		guessingSound.volume = volume;
		newWordSound.volume = volume;
		clockSound.volume = volume;
		writingSound.volume = volume;
	}
}

function setBackMusicVolume(volume){
	if(Device.isiOS()){
		sendRequest('musicVolume', volume);
	} else {
		backMusic.volume = volume;
	}
}

function playBackMusic(){
	if(isMusic){
		if(Device.isiOS()){
			sendRequest('play', backMusic);
		} else {	
			backMusic.play();
		}
	}
}

function playSound(sound){
	if(isSound){
		if(Device.isiOS()){
			sendRequest('play', sound);
		} else {	
			sound.play();
		}
	}
}

function pauseSound(sound){
	if(Device.isiOS()){
		sendRequest('stop', sound);
	} else {
		sound.pause();
	}
}

$(document).ready(function() {
	if(!Device.isiOS()){
		clockSound.setAttribute("onended", "this.play();");
		backMusic.setAttribute("onended", "this.play();");
	}
	setTimeout(function(){
		//backMusic.setAttribute("loop", "loop");
		isMusic = localStorage.getItem('music') != 'false';
		playBackMusic();
		if(!isMusic) $(".sound_check[tag = music]").addClass('active');
	}, 100);

	
	$(".sound_check").bind(isSupportTouch() ? "touchstart" : "mousedown", function(){//click(function(){
		if($(this).hasClass("active")){
			if($(this).attr("tag") == "music") {
				isMusic = true;
				playBackMusic();
				localStorage.setItem('music', true);
			}
			$(this).removeClass("active");
		} else {
			if($(this).attr("tag") == "music") {
				pauseSound(backMusic);
				localStorage.setItem('music', false);
			}
			$(this).addClass("active");
		}
		if($(this).attr("tag") == "sound"){
			isSound = !$(this).hasClass("active");
			localStorage.setItem('sound', isSound);
		}
	});	
	
	$(".button").click(function(){
		var id = $(this).attr("id");
		if(id != "game_btn_play" && id != "game_btn_guess" && id != "game_btn_hat" )
			playSound(buttonSound);
	});
});
